package com.bhatni.messageprocessor.macro;

import com.bhatni.messageprocessor.Message;
import com.bhatni.messageprocessor.macro.BaseMacro;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * Get the macro value from the regexp applied to the incoming content.
 */
public class RegexValue extends BaseMacro {

    private Pattern pattern;

    @Override
    public void setValue(String value) {
        this.pattern = Pattern.compile(value);
    }

    @Override
    public String getResolvedValue(Message in) {
        Matcher matcher = pattern.matcher(in.getBody().toString());
        if (matcher.matches()) {
            return matcher.group();
        } else {
            return "";
        }
    }

}
